﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using MethodWorx.CMS.Plugins.ActionResults;
using MethodWorx.CMS.Plugins.ContentTree;

namespace MethodWorx.CMS.Plugins.ViewManagement
{
    [CmsPlugin(Name = "View Management", Description = "Extension for managing views (collections of) data", Id = "{D3E94D2B-36AE-47E1-8BF0-41DFF6C8F6FB}")]
    public class ViewManagementExtension : Plugin, IContentTreePlugin, IAutoLoadedPlugin, IIdePlugin
    {

        public static Guid GetPluginId()
        {
            return new Guid("{D3E94D2B-36AE-47E1-8BF0-41DFF6C8F6FB}");
        }

        private IdeExtension ideExtension = new IdeExtension()
        {
            Id = new Guid("{4E004E6D-5401-4B21-B46D-69FDF203C5B4}"),
            DisplayName = "Create View",
            Name = "Create View",
            WebEditor = new AssemblyResourceReference(typeof(ViewManagementExtension).Assembly, "Plugins/Views/ViewManagement/Create.ascx"),
            Controller = typeof(ViewManagementController)
        };

        public IEnumerable<TreeNode> GetTreeNodes(string objectType, Guid? objectId)
        {
            if(this.IsRootNode(objectType, objectId))
            {
                //
                //  ok, return a node
                yield return new TreeNode()
                                 {
                                     Closed = true,
                                     Icon = "~/images/icons/folder.png",
                                     ObjectId = null,
                                     ObjectType = "Views",
                                     Title = "Views",
                                     ToolbarButtons = new ToolbarButton[]
                                                          {
                                                            new ToolbarButton()
                                                                {
                                                                    Id = "CreateView",
                                                                    Title = "Create View",
                                                                    ContextText = "Create View",
                                                                    Icon = "~/images/icons/table_add.png"
                                                                }
                                                            }
                                 };
            }
            else if(objectType == "Views" && objectId == null)
            {
                var service = new StaticViewService(this.CmsDataProvider);
                foreach(var sv in service.GetAll())
                {
                    yield return new TreeNode()
                                     {
                                         Closed = true,
                                         Icon = "~/images/icons/table.png",
                                         ObjectId = sv.Id,
                                         ObjectType = "StaticView",
                                         Title = sv.Name, 
                                         DefaultAction = "EditView",
                                         ToolbarButtons = new ToolbarButton []
                                         {
                                             new ToolbarButton()
                                             {
                                                 Id = "EditView",
                                                 Icon = "~/images/icons/table_edit.png",
                                                 Title = "Edit View"
                                             }
                                         }
                                     };
                }
            }
        }

        public System.Web.Mvc.ActionResult InvokeAction(string action, string objectType, Guid? objectId)
        {
            if(objectType == "Views" && objectId == null)
            {
                //
                //  ok, this is invoked from our views menu
                switch(action)
                {
                    case "CreateView":
                        //
                        //  right, lets go
                        return new OpenDialogResult(PluginUrlHelper.GlobalPluginAction(this.ideExtension, "Create"), "Create View");
                        break;
                }
            }
            else if (objectType == "StaticView")
            {
                return new OpenDialogResult(PluginUrlHelper.GlobalPluginAction(this.ideExtension, "Edit/" + objectId.ToString()), "Edit View");
            }


            return new EmptyResult();
        }

        public IEnumerable<IdeExtension> GetIdeExtensionsForPageTemplate(Content.PageTemplate template)
        {
            return new IdeExtension[0];
        }

        public IEnumerable<IdeExtension> GetIdeExtensionsForPage(Content.Page page)
        {
            return new IdeExtension[0];
        }

        public IEnumerable<IdeExtension> GetIdeExtensions()
        {
            yield return this.ideExtension;
        }
    }
}
